package Persistencia;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Vector;

import Controlador.Utils;
import Negocio.Cereal;
import Negocio.CerealCosechaGruesa;


public class AdmPersistenciaCerealCosechaGruesa extends AdministradorPersistencia 
{
	private static AdmPersistenciaCerealCosechaGruesa instancia;
	
	private AdmPersistenciaCerealCosechaGruesa()
	{
		
	}
	public static AdmPersistenciaCerealCosechaGruesa getInstancia()
	{
		if (instancia == null)
			instancia = new AdmPersistenciaCerealCosechaGruesa();
		return instancia;
	}


	public void delete(Object d) 
	{
		try
		{
			CerealCosechaGruesa a = (CerealCosechaGruesa)d;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cosechaGruesa set estado = ? where codigo = ?");
			s.setString(1, Utils.CONSTANTE_INACTIVO);
			s.setLong(2, a.getCodigo());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		

	}

	@Override
	public void insert(Object o) 
	{
		try
		{
			CerealCosechaGruesa a = (CerealCosechaGruesa)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("insert into cosechaGruesa values (?,?,?,?,?,?,?)");
			//agregar campos
			s.setLong(1,a.getCodigo());
			s.setString(2, a.getNombre());
			s.setDate(3, Date.valueOf(a.getInicioEpocaSiembra()));
			s.setDate(4, Date.valueOf(a.getFinEpocaSiembra()));
			s.setDouble(5,a.getRinde());
			s.setString(6, a.getEstado());
			s.setDouble(7, a.getDesperdicio());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		

	}

	@Override
	public Vector select(Object o) 
	{
		// TODO Auto-generated method stub
		return null;
	}
	//NO ES NECESARIO PORQUE YA LO HACE EN COSECHA FINA
	/*public int generarCodigo()
	{
		try
		{
		int cant =0, cant2 = 0;
		Connection con = PoolConnection.getPoolConnection().getConnection();
		PreparedStatement s = con.prepareStatement("Select Count(*) from cosechaGruesa");
		PreparedStatement r = con.prepareStatement("Select Count(*) from cosechaFina");
		ResultSet res=  s.executeQuery();
		ResultSet res2 = r.executeQuery();
		while (res.next())
		{
			cant = res.getInt(1);
		}
		while (res2.next())
		{
			cant2 = res.getInt(1);
		}
		PoolConnection.getPoolConnection().realeaseConnection(con);
		return cant + cant2;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			
		}
		return 0;
		
		
	}*/
	public void update(Object o) 
	{
		try
		{
			CerealCosechaGruesa a = (CerealCosechaGruesa)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cosechaGruesa set nombre = ?, inicioEpoca = ?, finEpoca = ?, rinde = ?, estado = ?, desperdicio = ? where codigo = ?" );
			
			//agregar campos
			s.setString(1, a.getNombre());
			
			s.setDate(2, Date.valueOf(a.getInicioEpocaSiembra()));
			s.setDate(3, Date.valueOf(a.getFinEpocaSiembra()));
			s.setDouble(4, a.getRinde());
			s.setString(5, a.getEstado());
			s.setDouble(6, a.getDesperdicio());
			
			s.setLong(7, a.getCodigo());
			
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		


	}
	public CerealCosechaGruesa buscarCerealCosechaGruesa(long codigo)
	{
		try
		{
			CerealCosechaGruesa a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from  cosechaGruesa where codigo = ?");			//agregar campos
			s.setLong(1,codigo);
			ResultSet result = s.executeQuery();
			while (result.next())
			{
				long cod = result.getLong(1);
				String nom = result.getString(2);
				String ini = result.getDate(3).toString();
				String fin = result.getDate(4).toString();
				double rinde = result.getDouble(5);
				String estado = result.getString(6);
				double desp = result.getDouble(7);
				a = new CerealCosechaGruesa(cod,nom,ini,fin,rinde,estado, desp );
				
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			System.out.println();
		}
		return null;
	}

}
